*****************************************
*	TITLE: 
*	More Options, but Less Willing to Cast a Valid Vote: Evidence from Electoral Reform in Chile
*
*	AUTHORS: 
*	Loreto Cox & Carmen Le Foulon
*
*	Replication file for:
*				 - Figures 2, 3, 5, 6, 7, 8, 9, 10, A3 & A4
*				 - Dataset for Figure 4
*				 - Tables 1, 2, 3, A1, A2, A3, A4, SM1, SM2, SM3 & SM4

* Modified on 03/06/2024 updating the code for Figures 2 and 3 to make them color blind. 
*****************************************

*--------------------------------------------------------------------------
* 							DIRECTORIES
*--------------------------------------------------------------------------
clear all
set more off

** Define your own directories 

	global data "./data"
	global figures "./figures"
	global tables "./tables"


ssc install asdoc
ssc install blindschemes
ssc install outreg2
ssc install ereplace
*--------------------------------------------------------------------------
* 								FIGURES
*--------------------------------------------------------------------------

*-----------
* FIGURE 2: Invalid votes as a percentage of total ballots cast in House and presidential elections, 1989–2021 
*-----------

use "$data/fig2_data",  clear

gen orden=_n
*INVALID
twoway (connected invalid_d2 orden, lcolor(blue) msymbol(T) mcolor(blue)) (connected invalid_p2 orden, lcolor(red) msymbol(S) mcolor(red)), scheme(plotplainblind) ytitle("Percent") xlabel(1(1)7 1 "1989" 2 "1993" 3 "2005" 4 "2009" 5 "2013" 6 "2017" 7 "2021") ylabel(0(2)10) legend(order(1 "House" 2 "Presidential")) name(invalid, replace) xtitle("") title("Total (blank+null)") xline(2.45, lpattern(dash)) xline(2.55, lpattern(dash)) 

*BLANK
twoway (connected blank_d2 orden, lcolor(blue) msymbol(T) mcolor(blue)) (connected blank_p2 orden, lcolor(red) msymbol(S) mcolor(red)), scheme(plotplainblind) ytitle("Percent") xlabel(1(1)7 1 "1989" 2 "1993" 3 "2005" 4 "2009" 5 "2013" 6 "2017" 7 "2021") ylabel(0(2)10) legend(off) name(blank, replace) xtitle("") title("Blank Votes") 

*NULL
twoway (connected null_d2 orden, lcolor(blue) msymbol(T) mcolor(blue)) (connected null_p2 orden, lcolor(red) msymbol(S) mcolor(red)), scheme(plotplainblind) ytitle("Percent") xlabel(1(1)7 1 "1989" 2 "1993" 3 "2005" 4 "2009" 5 "2013" 6 "2017" 7 "2021") ylabel(0(2)10) legend(off) name(null2, replace) xtitle("") title("Null Votes") 

graph combine invalid , rows(1) name(row1, replace) imargin(30 30 0 0)
graph combine blank null2 , rows(1) name(row2, replace)

graph combine row1 row2,  scheme(plotplainblind) col(1) graphregion(color(white))
graph export "$figures/figure2.png", replace


*-----------
* FIGURE 3: Invalid votes as a percentage of total ballots cast in Senate vs. presidential elections, 1989–2017 
*-----------
use "$data/district_votes",  clear

gen invalidosSEN1989=(votosblancos_sen_1989+votosnulos_sen_1989)
gen invalidosSEN1993=(votosblancos_sen_1993+votosnulos_sen_1993)
gen invalidosSEN2009=(votosblancos_sen_2009+votosnulos_sen_2009)
gen invalidosSEN2017=(votosblancos_sen_2017+votosnulos_sen_2017)

gen invalidosPRES1989=(votosblancos_pres_1989+votosnulos_pres_1989)
gen invalidosPRES1993=(votosblancos_pres_1993+votosnulos_pres_1993)
gen invalidosPRES2009=(votosblancos_pres_2009+votosnulos_pres_2009)
gen invalidosPRES2017=(votosblancos_pres_2017+votosnulos_pres_2017)

keep invalidosS* invalidosP* votosblancos_pres_1989 votosnulos_pres_1989 votosblancos_pres_1993 votosnulos_pres_1993 votosblancos_pres_2009 votosnulos_pres_2009 votosblancos_pres_2017 votosnulos_pres_2017 votosblancos_sen_1989 votosnulos_sen_1989 votosblancos_sen_1993 votosnulos_sen_1993 votosblancos_sen_2009 votosnulos_sen_2009 votosblancos_sen_2017 votosnulos_sen_2017 votostotales_sen_1989 votostotales_sen_1993 votostotales_sen_2009 votostotales_sen_2017 votostotales_pres_1989 votostotales_pres_1993 votostotales_pres_2009 votostotales_pres_2017 

collapse (sum) invalidosS* invalidosP* votosblancos_pres_1989 votosnulos_pres_1989 votosblancos_pres_1993 votosnulos_pres_1993 votosblancos_pres_2009 votosnulos_pres_2009 votosblancos_pres_2017 votosnulos_pres_2017 votosblancos_sen_1989 votosnulos_sen_1989 votosblancos_sen_1993 votosnulos_sen_1993 votosblancos_sen_2009 votosnulos_sen_2009 votosblancos_sen_2017 votosnulos_sen_2017 votostotales_sen_1989 votostotales_sen_1993 votostotales_sen_2009 votostotales_sen_2017 votostotales_pres_1989 votostotales_pres_1993 votostotales_pres_2009 votostotales_pres_2017 

rename (votosblancos_pres* votosblancos_sen* votosnulos_pres* votosnulos_sen* votostotales_pres* votostotales_sen*) (bp* bs* np* ns* tp* ts*)
gen aux=1
reshape long invalidosPRES invalidosSEN bp_ bs_ np_ ns_ tp_ ts_ , i(aux) j(año)
drop aux

gen invalid_s=invalidosSEN*100/ts_
gen invalid_p=invalidosPRES*100/tp_
gen blancos_s=bs_*100/ts_
gen blancos_p=bp_*100/tp_
gen nulos_s=ns_*100/ts_
gen nulos_p=np_*100/tp_
drop invalidosPRES invalidosSEN 
keep nulos* invali* blancos* año

gen orden=_n
*INVALID
twoway (connected invalid_s orden, lcolor(blue) msymbol(T) mcolor(blue)) (connected invalid_p orden, lcolor(red) msymbol(S) mcolor(red)), scheme(plotplainblind) ytitle("Percent") xlabel(1(1)4 1 "1989" 2 "1993" 3 "2009" 4 "2017") ylabel(0(2)10) legend(order(1 "Senate" 2 "Presidential")) name(invalid, replace) xtitle("") title("Total (blank+null)") xline(2.45, lpattern(dash)) xline(2.55, lpattern(dash)) xline(3.45, lpattern(dash)) xline(3.55, lpattern(dash)) 

*BLANK
twoway (connected blancos_s orden, lcolor(blue) msymbol(T) mcolor(blue)) (connected blancos_p orden, lcolor(red) msymbol(S) mcolor(red)), scheme(plotplainblind) ytitle("Percent") xlabel(1(1)4 1 "1989" 2 "1993" 3 "2009" 4 "2017") ylabel(0(2)6) legend(off) name(blank, replace) xtitle("") title("Blank Votes")

*NULL
twoway (connected nulos_s orden, lcolor(blue) msymbol(T) mcolor(blue)) (connected nulos_p orden, lcolor(red) msymbol(S) mcolor(red)), scheme(plotplainblind) ytitle("Percent") xlabel(1(1)4 1 "1989" 2 "1993" 3 "2009" 4 "2017") ylabel(0(2)6) legend(off) name(null2, replace) xtitle("") title("Null Votes")

graph combine invalid , rows(1) name(row1, replace)  imargin(30 30 0 0)
graph combine blank null2 , rows(1) name(row2, replace)

graph combine row1 row2, scheme(plotplainblind) col(1) graphregion(color(white))
graph export "$figures/figure3.png", replace

*-----------
* FIGURE 5: Betas for relationship between House vs. President DiDs of invalid voting and district magnitude
*-----------
use "$data/district_votes",  clear

merge 1:1 dip_dist_ant_id using "$data/fig5_data"

gen invalidos_dip_05=(votosblancos_dip_2005+votosnulos_dip_2005)*100/votostotales_dip_2005
gen invalidos_dip_09=(votosblancos_dip_2009+votosnulos_dip_2009)*100/votostotales_dip_2009
gen invalidos_dip_13=(votosblancos_dip_2013+votosnulos_dip_2013)*100/votostotales_dip_2013
gen invalidos_dip_17=(votosblancos_dip_2017+votosnulos_dip_2017)*100/votostotales_dip_2017
gen invalidos_dip_21=(iv_dip_21)*100/votostotales_dip_2021

gen invalidos_pres_05=(votosblancos_pres_2005+votosnulos_pres_2005)*100/votostotales_pres_2005
gen invalidos_pres_09=(votosblancos_pres_2009+votosnulos_pres_2009)*100/votostotales_pres_2009
gen invalidos_pres_13=(votosblancos_pres_2013+votosnulos_pres_2013)*100/votostotales_pres_2013
gen invalidos_pres_17=(votosblancos_pres_2017+votosnulos_pres_2017)*100/votostotales_pres_2017
gen invalidos_pres_21=(iv_pres_21)*100/votostotales_pres_2021


gen dif5=invalidos_dip_05-invalidos_pres_05
gen dif9=invalidos_dip_09-invalidos_pres_09
gen dif13=invalidos_dip_13-invalidos_pres_13
gen dif17=invalidos_dip_17-invalidos_pres_17
gen dif21=invalidos_dip_21-invalidos_pres_21

rename (dip_dist_ant_id dip_dist_seats) (distrito magnitud)

keep distrito magnitud dif* votostotales_pres_2005 votostotales_pres_2009 votostotales_pres_2013 votostotales_pres_2017 votostotales_pres_2021
rename (votostotales_pres_2005 votostotales_pres_2009 votostotales_pres_2013 votostotales_pres_2017 votostotales_pres_2021) (votostotales_pres_5 votostotales_pres_9 votostotales_pres_13 votostotales_pres_17 votostotales_pres_21)

reshape long dif votostotales_pres_, i(distrito) j(diff)
replace magnitud=magnitud-2

rename diff year

matrix define B=J(5,3,0)

reg dif magnitud  if year==5 
matrix R=r(table)
matrix B[1,1] = _b[magnitud]
matrix B[1,2] = R[5,1] // IC_lower
matrix B[1,3] = R[6,1] // IC_upper
reg dif magnitud  if year==9 
matrix R=r(table)
matrix B[2,1] = _b[magnitud]
matrix B[2,2] = R[5,1] // IC_lower
matrix B[2,3] = R[6,1] // IC_upper
reg dif magnitud  if year==13 
matrix R=r(table)
matrix B[3,1] = _b[magnitud]
matrix B[3,2] = R[5,1] // IC_lower
matrix B[3,3] = R[6,1] // IC_upper
reg dif magnitud  if year==17
matrix R=r(table)
matrix B[4,1] = _b[magnitud]
matrix B[4,2] = R[5,1] // IC_lower
matrix B[4,3] = R[6,1] // IC_upper
reg dif magnitud  if year==21 
matrix R=r(table)
matrix B[5,1] = _b[magnitud]
matrix B[5,2] = R[5,1] // IC_lower
matrix B[5,3] = R[6,1] // IC_upper

matlist B
svmat B, names(x)
keep x*
rename (x1 x2 x3) (magnitud lowIC upperIC)
gen orden=_n
drop if orden>5


twoway (scatter magnitud orden) (rcap lowIC upperIC orden), scheme(plotplainblind) xlabel(1 "2005" 2 "2009" 3 "2013" 4 "2017" 5 "2021") ytitle("OLS estimates") xtitle("Years") yline(0, lpatter(dash) lcolor(red)) legend(order(1 "Beta" 2 "CI at 95%"))
graph export "$figures/figure5.png", replace

*-----------
* FIGURE 6: Placebo check – difference in the share of invalid voting between House and presidential elections, by change in district magnitude (pre vs. post reform)
*-----------
use "$data/district_votes",  clear

merge 1:1 dip_dist_ant_id using "$data/fig5_data"

gen invalidos_dip_05=(votosblancos_dip_2005+votosnulos_dip_2005)*100/votostotales_dip_2005
gen invalidos_dip_09=(votosblancos_dip_2009+votosnulos_dip_2009)*100/votostotales_dip_2009
gen invalidos_dip_13=(votosblancos_dip_2013+votosnulos_dip_2013)*100/votostotales_dip_2013
gen invalidos_dip_17=(votosblancos_dip_2017+votosnulos_dip_2017)*100/votostotales_dip_2017
gen invalidos_dip_21=(iv_dip_21)*100/votostotales_dip_2021

gen invalidos_pres_05=(votosblancos_pres_2005+votosnulos_pres_2005)*100/votostotales_pres_2005
gen invalidos_pres_09=(votosblancos_pres_2009+votosnulos_pres_2009)*100/votostotales_pres_2009
gen invalidos_pres_13=(votosblancos_pres_2013+votosnulos_pres_2013)*100/votostotales_pres_2013
gen invalidos_pres_17=(votosblancos_pres_2017+votosnulos_pres_2017)*100/votostotales_pres_2017
gen invalidos_pres_21=(iv_pres_21)*100/votostotales_pres_2021


gen dif5=invalidos_dip_05-invalidos_pres_05
gen dif9=invalidos_dip_09-invalidos_pres_09
gen dif13=invalidos_dip_13-invalidos_pres_13
gen dif17=invalidos_dip_17-invalidos_pres_17
gen dif21=invalidos_dip_21-invalidos_pres_21

rename (dip_dist_ant_id dip_dist_seats) (distrito magnitud)

keep distrito magnitud dif* votostotales_pres_2005 votostotales_pres_2009 votostotales_pres_2013 votostotales_pres_2017 votostotales_pres_2021
rename (votostotales_pres_2005 votostotales_pres_2009 votostotales_pres_2013 votostotales_pres_2017 votostotales_pres_2021) (votostotales_pres_5 votostotales_pres_9 votostotales_pres_13 votostotales_pres_17 votostotales_pres_21)

reshape long dif votostotales_pres_, i(distrito) j(diff)
replace magnitud=magnitud-2

twoway (lfit dif magnitud  if diff==5, lcolor(gs14) lpatter(dash)) (lfit dif magnitud  if diff==9, lcolor(gs10) lpatter(shortdash)) (lfit dif magnitud  if diff==13, lcolor(gs7) lpatter(dot) lwidth(thick)) (lfit dif magnitud  if diff==17, lcolor(gs4) lpatter(solid)) (lfit dif magnitud  if diff==21, lpatter(dash) lcolor(black)) (scatter dif magnitud if diff==5, mcolor(gs14) msymbol(o)) (scatter dif magnitud if diff==9, mcolor(gs10) msymbol(+)) (scatter dif magnitud if diff==13, mcolor(gs7) msymbol(s)) (scatter dif magnitud if diff==17, mcolor(gs4) msymbol(dh)) (scatter dif magnitud if diff==21, mcolor(black) msymbol(oh)), scheme(plotplainblind) xtitle("Change in district magnitude") ytitle("Percentage points") legend(order( 6 "" 1 "2005" 7 "" 2 "2009" 8 "" 3 "2013" 9 "" 4 "2017" 10 "" 5 "2021") col(2)) 
graph export "$figures/figure6.png", replace

*-----------
* FIGURE 7: DiD in invalid voting between the House and Senate, by change in House–Senate difference in district magnitude, 2017–2009
*-----------
use "$data/district_votes",  clear

g magnitud=dip_dist_seats-sen_dist_seats
gen invalidos_sen_09=(votosblancos_sen_2009+votosnulos_sen_2009)*100/votostotales_sen_2009
gen invalidos_sen_17=(votosblancos_sen_2017+votosnulos_sen_2017)*100/votostotales_sen_2017
gen invalidos_dip_09=(votosblancos_dip_2009+votosnulos_dip_2009)*100/votostotales_dip_2009
gen invalidos_dip_13=(votosblancos_dip_2013+votosnulos_dip_2013)*100/votostotales_dip_2013
gen invalidos_dip_17=(votosblancos_dip_2017+votosnulos_dip_2017)*100/votostotales_dip_2017

gen votosblancos_sen_09=(votosblancos_sen_2009)*100/votostotales_sen_2009
gen votosblancos_sen_17=(votosblancos_sen_2017)*100/votostotales_sen_2017
gen votosblancos_dip_09=(votosblancos_dip_2009)*100/votostotales_dip_2009
gen votosblancos_dip_13=(votosblancos_dip_2013)*100/votostotales_dip_2013
gen votosblancos_dip_17=(votosblancos_dip_2017)*100/votostotales_dip_2017

gen votosnulos_sen_09=(votosnulos_sen_2009)*100/votostotales_sen_2009
gen votosnulos_sen_17=(votosnulos_sen_2017)*100/votostotales_sen_2017
gen votosnulos_dip_09=(votosnulos_dip_2009)*100/votostotales_dip_2009
gen votosnulos_dip_13=(votosnulos_dip_2013)*100/votostotales_dip_2013
gen votosnulos_dip_17=(votosnulos_dip_2017)*100/votostotales_dip_2017

gen DID17_iv=(invalidos_dip_17-invalidos_dip_09)-(invalidos_sen_17-invalidos_sen_09)
gen DID17_iv_B=(votosblancos_dip_17-votosblancos_dip_09)-(votosblancos_sen_17-votosblancos_sen_09)
gen DID17_iv_N=(votosnulos_dip_17-votosnulos_dip_09)-(votosnulos_sen_17-votosnulos_sen_09)
rename dip_dist_ant_id distrito 
keep distrito DID17_iv* magnitud votostotales_dip_2017 votostotales_sen_2017
drop if DID17_iv==.
rename votostotales_dip_2017 votos


twoway (scatter DID17_iv magnitud [fw=votos], mcolor(blue) msize(vsmall)) (lfit DID17_iv magnitud, lcolor(red) lpattern(solid)) (qfit DID17_iv magnitud, lcolor(red) lpatter(dash)) , scheme(plotplainblind) ytitle("Difference in percentage") xtitle("Change in House-Senate difference in district magnitude (2017-2009)") title("Total (Blank+Null)")  legend(off) name(invalid, replace) text(4.2 -0.6 "Beta=0.67", color(black) s(small)) ylabel(-3 (1) 5)

twoway (scatter DID17_iv_B magnitud [fw=votos], mcolor(blue) msize(vsmall)) (lfit DID17_iv_B magnitud, lcolor(red) lpattern(solid)) (qfit DID17_iv_B magnitud, lcolor(red) lpatter(dash)) , scheme(plotplainblind) ytitle("Difference in percentage") xtitle("Change in House-Senate difference in district magnitude (2017-2009)") title("Blank Votes")  legend(off) name(blank, replace) text(4.2 -0.6 "Beta=0.54", color(black) s(small)) ylabel(-3 (1) 5)

twoway (scatter DID17_iv_N magnitud [fw=votos], mcolor(blue) msize(vsmall)) (lfit DID17_iv_N magnitud, lcolor(red) lpattern(solid)) (qfit DID17_iv_N magnitud, lcolor(red) lpatter(dash)) , scheme(plotplainblind) ytitle("Difference in percentage") xtitle("Change in House-Senate difference in district magnitude (2017-2009)") title("Null Votes")  legend(off) name(null2, replace) text(4.2 -0.6 "Beta=0.13", color(black) s(small))  ylabel(-3 (1) 5)

graph combine invalid , rows(1) name(row1, replace)
graph combine blank null2 , rows(1) name(row2, replace)

reg DID17_iv magnitud [fw=votos]
reg DID17_iv_B magnitud [fw=votos]
reg DID17_iv_N magnitud [fw=votos]

graph combine row1 row2,  scheme(plotplainblind) col(1) graphregion(color(white)) ycommon
graph export "$figures/figure7.png", replace

*-----------
* FIGURE 8: Placebo check – difference in the share of invalid voting between House and Senate elections, by the House–Senate post-reform difference in district magnitude
*-----------
use "$data/district_votes",  clear

merge 1:1 dip_dist_ant_id using "$data/fig5_data"
drop _merge
merge 1:1 dip_dist_ant_id using "$data/fig8_data"
drop _merge

gen invalidos_dip_05=(votosblancos_dip_2005+votosnulos_dip_2005)*100/votostotales_dip_2005
gen invalidos_dip_09=(votosblancos_dip_2009+votosnulos_dip_2009)*100/votostotales_dip_2009
gen invalidos_dip_13=(votosblancos_dip_2013+votosnulos_dip_2013)*100/votostotales_dip_2013
gen invalidos_dip_17=(votosblancos_dip_2017+votosnulos_dip_2017)*100/votostotales_dip_2017
gen invalidos_dip_21=(iv_dip_21)*100/votostotales_dip_2021

gen invalidos_sen_05=(votosblancos_sen_2005+votosnulos_sen_2005)*100/votostotales_sen_2005
gen invalidos_sen_09=(votosblancos_sen_2009+votosnulos_sen_2009)*100/votostotales_sen_2009
gen invalidos_sen_13=(votosblancos_sen_2013+votosnulos_sen_2013)*100/votostotales_sen_2013
gen invalidos_sen_17=(votosblancos_sen_2017+votosnulos_sen_2017)*100/votostotales_sen_2017
gen invalidos_sen_21=(iv_sen_21)*100/votostotales_sen_2021

gen dif5=invalidos_dip_05-invalidos_sen_05
gen dif9=invalidos_dip_09-invalidos_sen_09
gen dif13=invalidos_dip_13-invalidos_sen_13
gen dif17=invalidos_dip_17-invalidos_sen_17
gen dif21=invalidos_dip_21-invalidos_sen_21

gen magnitud=dip_dist_seats-sen_dist_seats

rename (dip_dist_ant_id) (distrito)
keep distrito magnitud dif* votostotales_pres_2005 votostotales_pres_2009 votostotales_pres_2013 votostotales_pres_2017 votostotales_pres_2021
rename (votostotales_pres_2005 votostotales_pres_2009 votostotales_pres_2013 votostotales_pres_2017 votostotales_pres_2021) (votostotales_pres_5 votostotales_pres_9 votostotales_pres_13 votostotales_pres_17 votostotales_pres_21)

reshape long dif votostotales_pres_, i(distrito) j(diff)

twoway (lfit dif magnitud  if diff==5, lcolor(gs12) lpatter(dash)) (lfit dif magnitud  if diff==9, lcolor(gs8) lpatter(shortdash)) (lfit dif magnitud  if diff==13, lcolor(gs6) lpatter(dot) lwidth(thick)) (lfit dif magnitud  if diff==17, lcolor(black) lpatter(solid)) (lfit dif magnitud  if diff==21, lcolor(black) lpatter(dash)) (scatter dif magnitud if diff==5, mcolor(gs12) msymbol(o)) (scatter dif magnitud if diff==9, mcolor(gs8) msymbol(+)) (scatter dif magnitud if diff==13, mcolor(gs6) msymbol(s)) (scatter dif magnitud if diff==17, mcolor(black) msymbol(dh)) (scatter dif magnitud if diff==21, mcolor(black) msymbol(oh)), scheme(plotplainblind) xtitle("Difference in district magnitude") ytitle("Percentage points") legend(order( 6 "" 1 "2005" 7 "" 2 "2009" 8 "" 3 "2013" 9 "" 4 "2017" 10 "" 5 "2021") col(2)) 
graph export "$figures/figure8.png", replace

*-----------
* FIGURE 9: Proportion of invalid votes by ballot size
*-----------


clear all
set obs 4
gen match=1 in 1
replace match=2 in 2
replace match=3 in 3
replace match=4 in 4
gen av1=.
gen top1=.
gen down1=.
forvalues i=1/4{
preserve
use "$data/survey_data", clear
mean invalidos if version==`i'
restore
matrix R=r(table)
replace av1=R[1,1] in `i'
replace down1=R[5,1] in `i'
replace top1=R[6,1] in `i'
}
gen invalidos=av1*100
gen ic_a=top1*100
gen ic_b=down1*100
rename match version
twoway (lfit invalidos version, clpattern(dash) clcolor(black)) (scatter invalidos version, msymbol(0) color(black)) (rcap ic_a ic_b version, lcolor(gs2)), scheme(plotplainblind)  xlabel(1 "25% Full (N=606)" 2 "50% Full (N=587)" 3 "75% Full (N=597)" 4 "Full (N=592)", labsize(small)) legend(off) ytitle("Invalid Votes (%)") xtitle("Ballot Type") aspectratio(0.65) ylabel(30(2)45, ang(45))
graph export "$figures/figure9.png", replace

*-----------
* FIGURE 10: Proportion of invalid votes by respondents' characteristics
*-----------

** Figure 10A

clear all
set obs 3
gen match=1 in 1
replace match=2 in 2
replace match=3 in 3
gen av1=.
gen top1=.
gen down1=.
forvalues i=1/3{
preserve
use "$data/survey_data", clear
rename invalidos invalid
gen invalidos=invalid*100
drop if version==.
xtile escq=esc, nq(3)
drop if esc==.
mean invalid if escq==`i'
restore
matrix R=r(table)
replace av1=R[1,1] in `i'
replace down1=R[5,1] in `i'
replace top1=R[6,1] in `i'
}
gen invalidos=av1*100
gen ic_a=top1*100
gen ic_b=down1*100
rename match escq
twoway (scatter invalidos escq, msymbol(0) color(black)) (lfit invalidos escq, clpattern(dash) clcolor(gs6)) (rcap ic_a ic_b escq, lcolor(black)), scheme(plotplainblind)  xlabel(1 "Lowest (N=828)" 2 "Middle (N=832)" 3 "Highest (N=709)") ytitle("Invalid Votes (%)") xtitle("Education tercile") legend(off) aspectratio(0.65)
graph export "$figures/figure10_A.png", replace


*** Figure 10B

clear all
set obs 5
gen match=1 in 1
replace match=2 in 2
replace match=3 in 3
replace match=4 in 4
replace match=5 in 5
gen av1=.
gen top1=.
gen down1=.
forvalues i=1/5{
preserve
use "$data/survey_data", clear
rename invalidos invalid
gen invalidos=invalid*100
drop if version==.
drop if nse==.
mean invalid if nse==`i'
restore
matrix R=r(table)
replace av1=R[1,1] in `i'
replace down1=R[5,1] in `i'
replace top1=R[6,1] in `i'
}
gen invalidos=av1*100
gen ic_a=top1*100
gen ic_b=down1*100
gen nse=-match+6
twoway (scatter invalidos nse, msymbol(0) color(black)) (lfit invalidos nse, clpattern(dash) clcolor(gs6)) (rcap ic_a ic_b nse, lcolor(black)), scheme(plotplainblind) xlabel(1 "Low (N=318)" 2 "Mid-Low (N=630)" 3 "Middle (N=506)" 4 "Mid-High (N=504)" 5 "High (N=420)") ytitle("Invalid Votes (%)") xtitle("SES") legend(off) aspectratio(0.65)
graph export "$figures/figure10_B.png", replace


*** Figure 10C

clear all
set obs 4
gen match=1 in 1
replace match=2 in 2
replace match=3 in 3
replace match=4 in 4
gen av1=.
gen top1=.
gen down1=.
forvalues i=1/4{
preserve
use "$data/survey_data", clear
rename invalidos invalid
gen invalidos=invalid*100
drop if version==.
drop if interes==.
mean invalid if interes==`i'
restore
matrix R=r(table)
replace av1=R[1,1] in `i'
replace down1=R[5,1] in `i'
replace top1=R[6,1] in `i'
}
gen invalidos=av1*100
gen ic_a=top1*100
gen ic_b=down1*100
gen interes=-match+5
twoway (scatter invalidos interes, msymbol(0) color(black)) (lfit invalidos interes, clpattern(dash) clcolor(gs6)) (rcap ic_a ic_b interes, lcolor(black)), scheme(plotplainblind) xlabel(1 "Not interested (N=586)" 2 "Somewhat (N=929)" 3 "Quite (N=604)" 4 "Very interested (N=257)") ytitle("Invalid Votes (%)") xtitle("Interest in politics") legend(off) aspectratio(0.65)
graph export "$figures/figure10_C.png", replace


*--------------------------------------------------------------------------
* 							FIGURES - APPENDIX
*--------------------------------------------------------------------------

*-----------
* FIGURES A3 & A4
*-----------

*---- 
* Datasets

** Lower house

foreach xx in dip pres sen {
foreach x of numlist 1989 1993 2005 2009 2013 2017 {
use "$data/district_votes",  clear
	    keep dip_dist_ant_id votosblancos_`xx'_`x' votosnulos_`xx'_`x' votostotales_`xx'_`x'
		rename (votosblancos_`xx'_`x' votosnulos_`xx'_`x' votostotales_`xx'_`x') (blancos nulos totales)
		gen `xx'=1
		gen año=`x'
		tempfile data_`xx'_`x'
		save `data_`xx'_`x''
	}
}


clear
foreach xx in dip pres sen{
foreach x of numlist 1989 1993 2005 2009 2013 2017{
    append using `data_`xx'_`x''
}
}

 append using "$data/tot21.dta"
 
 replace año=2021 if year==2021
 
 gen treat=0 
replace treat=1 if dip==1 

gen time1=0
replace time1=1 if año==1993
gen time2=0
replace time2=1 if año==2005
gen time3=0
replace time3=1 if año==2009
gen time4=0
replace time4=1 if año==2013
gen time5=0
replace time5=1 if año==2017
gen time6=0
replace time6=1 if año==2021

gen did1=time1*treat
gen did2=time2*treat
gen did3=time3*treat
gen did4=time4*treat
gen did5=time5*treat
gen did6=time6*treat

drop if sen==1
save "$data/data_figA3A4_dip.dta", replace

** Senate

foreach xx in dip pres sen {
foreach x of numlist 1989 1993 2005 2009 2013 2017 {
use "$data/district_votes",  clear
	    keep dip_dist_ant_id votosblancos_`xx'_`x' votosnulos_`xx'_`x' votostotales_`xx'_`x'
		rename (votosblancos_`xx'_`x' votosnulos_`xx'_`x' votostotales_`xx'_`x') (blancos nulos totales)
		gen `xx'=1
		gen año=`x'
		tempfile data_`xx'_`x'
		save `data_`xx'_`x''
	}
}


clear
foreach xx in dip pres sen{
foreach x of numlist 1989 1993 2005 2009 2013 2017{
    append using `data_`xx'_`x''
}
}

 append using "$data/tot21.dta"
 
replace año=2021 if year==2021

drop if dip==1
gen treat=0 
replace treat=1 if sen==1 

gen time1=0
replace time1=1 if año==1993
gen time2=0
replace time2=1 if año==2005
gen time3=0
replace time3=1 if año==2009
gen time4=0
replace time4=1 if año==2013
gen time5=0
replace time5=1 if año==2017
gen time6=0
replace time6=1 if año==2021

gen did1=time1*treat
gen did2=time2*treat
gen did3=time3*treat
gen did4=time4*treat
gen did5=time5*treat
gen did6=time6*treat

save "$data/data_figA3A4_sen.dta", replace




*-----------
* FIGURE A3_A: DiD estimators invalid votes for House vs. presidential elections
*-----------

use "$data/data_figA3A4_dip", clear

gen invalid=(blancos+nulos)*100/totales

matrix define B=J(6,3,0)
reg invalid treat time1 did1 if año<=1993
matrix R=r(table)
matrix B[1,1] = _b[did1]
matrix B[1,2] = R[5,3] // IC_lower
matrix B[1,3] = R[6,3] // IC_upper
reg invalid treat time2 did2 if año>=1993 & año<=2005
matrix R=r(table)
matrix B[2,1] = _b[did2]
matrix B[2,2] = R[5,3] // IC_lower
matrix B[2,3] = R[6,3] // IC_upper
reg invalid treat time3 did3 if año>=2005 & año<=2009
matrix R=r(table)
matrix B[3,1] = _b[did3]
matrix B[3,2] = R[5,3] // IC_lower
matrix B[3,3] = R[6,3] // IC_upper
reg invalid treat time4 did4 if año>=2009 & año<=2013
matrix R=r(table)
matrix B[4,1] = _b[did4]
matrix B[4,2] = R[5,3] // IC_lower
matrix B[4,3] = R[6,3] // IC_upper
reg invalid treat time5 did5 if año>=2013 & año<=2017 
matrix R=r(table)
matrix B[5,1] = _b[did5]
matrix B[5,2] = R[5,3] // IC_lower
matrix B[5,3] = R[6,3] // IC_upper
reg invalid treat time6 did6 if año==2017 | año==2021
matrix R=r(table)
matrix B[6,1] = _b[did6]
matrix B[6,2] = R[5,3] // IC_lower
matrix B[6,3] = R[6,3] // IC_upper

matlist B
svmat B, names(x)
keep x*
rename (x1 x2 x3) (DID lowIC upperIC)
gen orden=_n
keep if orden<7

twoway (scatter DID orden) (rcap lowIC upperIC orden), scheme(plotplainblind) xlabel(1 "89-93" 2 "93-05" 3 "05-09" 4 "09-13" 5 "13-17" 6 "17-21") ytitle("DiD estimates for invalid votes (%)") xtitle("Years") title("") yline(0, lpatter(dash) lcolor(red)) legend(order(1 "DiD" 2 "CI at 95%"))
graph export "$figures/figureA3_HousevsPres.png", replace

*-----------
* FIGURE A4: DiD estimators for Senate vs. presidential elections
*-----------

use "$data/data_figA3A4_sen.dta", clear


gen invalid=(blancos+nulos)*100/totales

matrix define B=J(7,3,0)
reg invalid treat time1 did1 if año==1993 | año==1989
matrix R=r(table)
matrix B[1,1] = _b[did1]
matrix B[1,2] = R[5,3] // IC_lower
matrix B[1,3] = R[6,3] // IC_upper

reg invalid treat time3 did3 if año==1993 | año==2009
matrix R=r(table)
matrix B[3,1] = _b[did3]
matrix B[3,2] = R[5,3] // IC_lower
matrix B[3,3] = R[6,3] // IC_upper

reg invalid treat time5 did5 if año==2009 | año==2017
matrix R=r(table)
matrix B[5,1] = _b[did5]
matrix B[5,2] = R[5,3] // IC_lower
matrix B[5,3] = R[6,3] // IC_upper

reg invalid treat time6 did6 if año==2013 | año==2021
matrix R=r(table)
matrix B[7,1] = _b[did6]
matrix B[7,2] = R[5,3] // IC_lower
matrix B[7,3] = R[6,3] // IC_upper*/

matlist B
svmat B, names(x)
keep x*
rename (x1 x2 x3) (DID lowIC upperIC)
gen orden=_n
keep if orden==1 | orden==3 | orden==5 | orden==7

twoway (scatter DID orden) (rcap lowIC upperIC orden), scheme(plotplainblind) xlabel(1 "89-93" 3 "93-09" 5 "09-17" 7 "13-21") ytitle("DiD estimates for invalid votes (%)") xtitle("Years") title("") yline(0, lpatter(dash) lcolor(red)) legend(order(1 "DiD" 2 "CI at 95%"))

graph export "$figures/figureA4.png", replace


*--------------------------------------------------------------------------
* 								TABLES
*--------------------------------------------------------------------------

*-----------
* TABLE 1: Regression DiD estimations
*-----------

use "$data/servel_data", clear

egen comuna_id=group(comuna)
xtset comuna_id // set FE by municipality

lab var treat "Election Type = Dip"
lab var post "Year = 2017"


xtreg invalidos treat post did, fe vce(cluster distrito_ant)
est sto x1
outreg2 using "$tables/reg_table.doc", label replace ctitle(Invalid) addtext(Municipality FE, Yes) dec(3)
xtreg invalidos treat post did 1.did#c.magnitud, fe vce(cluster distrito_ant)
est sto x2
outreg2 using "$tables/reg_table.doc", label append ctitle(Invalid) addtext(Municipality FE, Yes) dec(3)
xtreg invalidos treat post 1.did#magnitud, fe vce(cluster distrito_ant)
est sto x3
outreg2 using "$tables/reg_table.doc", label append ctitle(Invalid) addtext(Municipality FE, Yes) dec(3)
xtreg nulos treat post did, fe vce(cluster distrito_ant)
est sto x4
outreg2 using "$tables/reg_table.doc", label append ctitle(Null) addtext(Municipality FE, Yes) dec(3)
xtreg nulos treat post did 1.did#c.magnitud, fe vce(cluster distrito_ant)
est sto x5
outreg2 using "$tables/reg_table.doc", label append ctitle(Null) addtext(Municipality FE, Yes) dec(3)
xtreg nulos treat post 1.did#magnitud, fe vce(cluster distrito_ant)
est sto x6
outreg2 using "$tables/reg_table.doc", label append ctitle(Null) addtext(Municipality FE, Yes) dec(3)
xtreg blancos treat post did, fe vce(cluster distrito_ant)
est sto x7
outreg2 using "$tables/reg_table.doc", label append ctitle(Blank) addtext(Municipality FE, Yes) dec(3)
xtreg blancos treat post did 1.did#c.magnitud, fe vce(cluster distrito_ant)
est sto x8
outreg2 using "$tables/reg_table.doc",label  append ctitle(Blank) addtext(Municipality FE, Yes) dec(3)
xtreg blancos treat post 1.did#magnitud, fe vce(cluster distrito_ant)
est sto x9
outreg2 using "$tables/reg_table.doc", label append ctitle(Blank) addtext(Municipality FE, Yes) dec(3)


*-----------
*** Dataset for Figure 4
*-----------

use "$data/servel_data", clear
lab var treat "Election Type = Dip"
lab var post "Year = 2017"
egen comuna_id=group(comuna)
xtset comuna_id // set FE by municipality

xtreg invalidos treat post i.did 1.did#c.magnitud, fe vce(cluster distrito_ant)

margins,  at(magnitud=(1 (1) 6) did=1 treat=1 post=1 ) 
margins,  at(magnitud=(1 (1) 6) did=0 treat=1 post=0 ) 

* using these values, create excel file pred_plot.xlsx

*-----------
* TABLE 2: DiDs by municipalities' quartiles of education and poverty
*-----------
preserve
use "$data/servel_data", clear
keep if año==2017
collapse esc, by(comuna)
drop if esc==.
tempfile esc17
save `esc17'
restore
use "$data/servel_data", clear
rename esc esc_1317
merge m:1 comuna using `esc17'
drop _merge


lab var treat "\textit{Election Type = Dip}"
lab var post "\textit{Year = 2017}"

xtile quartil_esc=esc , n(4)
xtile quartil_p=pobreza_multi_5d_17 , n(4)
egen comuna_id=group(comuna)
xtset comuna_id // set FE by municipality

xtreg invalidos treat post 1.did##quartil_esc , fe vce(cluster distrito_ant)
est sto ed
outreg2 using "$tables/table2.doc", label replace keep(treat post 1.did 1.did#2.quartil_esc 1.did#3.quartil_esc 1.did#4.quartil_esc) ctitle(By education) addtext(Municipality FE, Yes) dec(3)
xtreg invalidos treat post 1.did##quartil_p , fe vce(cluster distrito_ant)
est sto pov
outreg2 using "$tables/table2.doc", label append ctitle(By poverty) keep(treat post 1.did 1.did#2.quartil_esc 1.did#3.quartil_esc 1.did#4.quartil_esc 1.did#2.quartil_p 1.did#3.quartil_p 1.did#4.quartil_p ) addtext(Municipality FE, Yes) dec(3)


*-----------
* TABLE 3: Treatment effects per ballot type for invalid, null, and blank votes 
*-----------
use "$data/survey_data", clear


reg invalidos MR SMR TMR, r 
est sto g1
outreg2 using "$tables/table3.doc",  keep(MR SMR TMR) label replace ctitle(Invalid) addtext(District FE, No, Controls, No) dec(3)

reg invalidos MR SMR TMR i.distrito, r 
est sto g2
outreg2 using "$tables/table3.doc", keep(MR SMR TMR)  label append ctitle(Invalid) addtext(District FE, Yes, Controls, No) dec(3)

reg invalidos MR SMR TMR i.distrito mujer esc edad i.nse i.interes, r 
est sto g3
outreg2 using "$tables/table3.doc", keep(MR SMR TMR)  label append ctitle(Invalid) addtext(District FE, Yes, Controls, Yes) dec(3)

reg nulos MR SMR TMR, r 
est sto g4
outreg2 using "$tables/table3.doc", keep(MR SMR TMR) label  append ctitle(Null) addtext(District FE, No, Controls, No) dec(3)
reg nulos MR SMR TMR i.distrito, r 
est sto g5
outreg2 using "$tables/table3.doc", keep(MR SMR TMR) label  append ctitle(Null) addtext(District FE, Yes, Controls, No) dec(3)
reg nulos MR SMR TMR i.distrito mujer esc edad i.nse i.interes, r 
est sto g6
outreg2 using "$tables/table3.doc", keep(MR SMR TMR)  label append ctitle(Null) addtext(District FE, Yes, Controls, Yes) dec(3)
reg blancos MR SMR TMR, r 
est sto g7
outreg2 using "$tables/table3.doc", keep(MR SMR TMR) label  append ctitle(Blank) addtext(District FE, No, Controls, No) dec(3)
reg blancos MR SMR TMR i.distrito, r 
est sto g8
outreg2 using "$tables/table3.doc", keep(MR SMR TMR)  label append ctitle(Blank) addtext(District FE, Yes, Controls, No) dec(3)
reg blancos MR SMR TMR i.distrito mujer esc edad i.nse i.interes, r 
est sto g9
outreg2 using "$tables/table3.doc", keep(MR SMR TMR) label  append ctitle(Blank) addtext(District FE, Yes, Controls, Yes) dec(3)



*--------------------------------------------------------------------------
* 							TABLES - APPENDIX
*--------------------------------------------------------------------------

*-----------
* TABLE A1: Descriptive statistics for main regressions
*-----------

preserve
use "$data/servel_data", clear
keep if año==2017
collapse esc, by(comuna)
drop if esc==.
tempfile esc17
save `esc17'
restore
use "$data/servel_data", clear
rename esc esc_1317
merge m:1 comuna using `esc17'
drop _merge

lab var treat "Treat"
lab var post "Post"
lab var did "DiD"
lab var esc "Education level"
lab var pobreza_multi_5d_17 "Poverty level"

asdoc summarize treat post did magnitud invalidos nulos blancos esc pobreza_multi_5d_17, replace save($tables/did_ds.doc) label 



*-----------
* TABLE A2: Descriptive statistics for survey experiment
*-----------
use "$data/survey_data", clear

gen prop_elim=prop_eliminados/100

asdoc summarize invalidos nulos blancos MR SMR TMR prop_elim edad mujer i.interes i.nivel i.area, replace save($tables/tableA1_A.doc) label sformat(%12.1f percent mean summ) 


*-----------
* TABLE A3: Balance table for survey experiment
*-----------
gen rural=0 
replace rural=1 if area==2
lab define rur 0 "Urban" 1 "Rural" 
lab values rural rur

reghdfe MR invalidos edad mujer rural b4.interes i.nivel i.nse, abs(distrito)
outreg2 using "$tables/balance.doc", replace ctitle(MR) label addtext(District FE, Yes) dec(3)
foreach y in SMR TMR FULL {
    reghdfe `y' invalidos edad mujer rural b4.interes i.nivel i.nse, abs(distrito)
	est sto r`y'
	outreg2 using "$tables/balance.doc", append ctitle(`y') label addtext(District FE, Yes) dec(3)
}


*-----------
* TABLE A4: Treatment effects per ballot type for null and blank votes controlling for proportion of eliminated votes
*-----------
use "$data/survey_data", clear
drop if version==.

reg invalidos MR SMR TMR prop_eliminados , r 
est sto g2
outreg2 using "$tables/tableA4.doc", replace ctitle(Invalid) keep(MR SMR TMR prop_eliminados) label addtext(District FE, No, Controls, No) dec(3)
reg invalidos MR SMR TMR i.distrito prop_eliminados mujer esc edad i.nse i.interes, r 
est sto g3
outreg2 using "$tables/tableA4.doc", append ctitle(Invalid) keep(MR SMR TMR prop_eliminados) label addtext(District FE, Yes, Controls, Yes) dec(3)
reg nulos MR SMR TMR prop_eliminados , r 
est sto g5
outreg2 using "$tables/tableA4.doc", append ctitle(Null) keep(MR SMR TMR prop_eliminados) label addtext(District FE, No, Controls, No) dec(3)
reg nulos MR SMR TMR i.distrito prop_eliminados mujer esc edad i.nse i.interes, r 
est sto g6
outreg2 using "$tables/tableA4.doc", append ctitle(Null) keep(MR SMR TMR prop_eliminados) label addtext(District FE, Yes, Controls, Yes) dec(3)
reg blancos MR SMR TMR prop_eliminados , r 
est sto g8
outreg2 using "$tables/tableA4.doc", append ctitle(Blank) keep(MR SMR TMR prop_eliminados) label addtext(District FE, No, Controls, No) dec(3)
reg blancos MR SMR TMR i.distrito prop_eliminados mujer esc edad i.nse i.interes, r 
est sto g9
outreg2 using "$tables/tableA4.doc", append ctitle(Blank)  keep(MR SMR TMR prop_eliminados) label addtext(District FE, Yes, Controls, Yes) dec(3)


*--------------------------------------------------------------------------
* 							SUPLEMENTARY MATERIAL
*--------------------------------------------------------------------------

*------------
* APPENDIX 3: Regression DiD estimations
*------------

use "$data/municipality_votes", clear


reshape long invalid_pres invalid_dip invalid_sen blank_pres blank_dip blank_sen null_pres null_dip null_sen votostotales_pres_ votostotales_dip_ votostotales_sen_, i(comuna) j(year)
rename (*_pres *_dip *_sen) (*_1 *_2 *_3)
rename (*_pres_ *_dip_ *_sen_) (*_1 *_2 *_3)
reshape long invalid_ blank_ null_ votostotales_ , i(comuna year) j(election)


*---
* Table SM1: Senate vs. Presidential election: Regression DiD estimations
*----

preserve
drop if election==2
gen treat=(election==3)
gen post=(year==2017)
gen did=post*treat
drop if comuna=="ALTO HOSPICIO" // * missing data for one presidential election


label var treat "Election type = Senate"
label var post "Year=2017"
label var did "Election type x Year"

reg invalid treat post did [w=votostotales_], r
est sto r1
outreg2 using "$tables/tableSM_A3_1.doc", replace label ctitle(Invalid) dec(3)
reg null treat post did [w=votostotales_], r
est sto r3
outreg2 using "$tables/tableSM_A3_1.doc", append label ctitle(Null) dec(3)
reg blank treat post did [w=votostotales_], r
est sto r2
outreg2 using "$tables/tableSM_A3_1.doc", append label ctitle(Blank) dec(3)


*---
* Table SM2: House vs. Senate elections: Regression DiD estimations
*----

preserve
drop if election==1
gen treat=(election==2)
gen post=(year==2017)
gen did=post*treat


label var treat "Election type = Senate"
label var post "Year=2017"
label var did "Election type x Year"

reg invalid treat post did [w=votostotales_], r
est sto r1
outreg2 using "$tables/tableSM_A3_2.doc",  replace label ctitle(Invalid) dec(3)
reg null treat post did [w=votostotales_], r
est sto r3
outreg2 using "$tables/tableSM_A3_2.doc", append label ctitle(Null) dec(3)
reg blank treat post did [w=votostotales_], r
est sto r2
outreg2 using "$tables/tableSM_A3_2.doc", append label ctitle(Blank) dec(3)


*------------
* APPENDIX 4: Survey experiment with weights
*------------

*---
* Table SM3: Survey experiment: Weighted results
*----



use "$data/survey_data", clear



reg invalidos MR SMR TMR i.distrito [pw=ponderador] if merge_ponderador==3, r 
est sto g2
outreg2 using "$tables/tableSM_A4_3.doc", replace keep(MR SMR TMR) label ctitle(Invalid) addtext(District FE, Yes, Individual Controls, No) dec(3)
reg invalidos MR SMR TMR i.distrito mujer esc edad i.nse i.interes [pw=ponderador] if merge_ponderador==3, r 
est sto g3
outreg2 using "$tables/tableSM_A4_3.doc", append keep(MR SMR TMR) label  ctitle(Invalid)  addtext(District FE, Yes, Individual Controls, Yes) dec(3)
reg nulos MR SMR TMR i.distrito [pw=ponderador] if merge_ponderador==3, r 
est sto g5
outreg2 using "$tables/tableSM_A4_3.doc", append keep(MR SMR TMR) label  ctitle(Null)  addtext(District FE, Yes, Individual Controls, No) dec(3)
reg nulos MR SMR TMR i.distrito mujer esc edad i.nse i.interes [pw=ponderador] if merge_ponderador==3, r 
est sto g6
outreg2 using "$tables/tableSM_A4_3.doc", append keep(MR SMR TMR) label  ctitle(Null)  addtext(District FE, Yes, Individual Controls, Yes) dec(3)
reg blancos MR SMR TMR i.distrito [pw=ponderador] if merge_ponderador==3, r 
est sto g8
outreg2 using "$tables/tableSM_A4_3.doc", append keep(MR SMR TMR) label  ctitle(Blank)  addtext(District FE, Yes, Individual Controls, No) dec(3)
reg blancos MR SMR TMR i.distrito mujer esc edad i.nse i.interes [pw=ponderador] if merge_ponderador==3, r 
est sto g9
outreg2 using "$tables/tableSM_A4_3.doc", append  keep(MR SMR TMR) label ctitle(Blank)  addtext(District FE, Yes, Individual Controls, Yes) dec(3)



*---
* Table SM4: Population and unweighted sample distributions
*----


use "$data/ponderadores.dta", clear


expand(2), gen(sample) 

egen urban = sum(total_grupo) if area==1 & sample==0
egen rural = sum(total_grupo) if area==2 & sample==0

egen male = sum(total_grupo) if sexo==1 & sample==0
egen female = sum(total_grupo) if sexo==2 & sample==0

egen ed1 = sum(total_grupo) if nivel==1 & sample==0
egen ed2 = sum(total_grupo) if nivel==2 & sample==0
egen ed3 = sum(total_grupo) if nivel==3 & sample==0

egen age1 = sum(total_grupo) if tramo==1 & sample==0
egen age2 = sum(total_grupo) if tramo==2 & sample==0
egen age3 = sum(total_grupo) if tramo==3 & sample==0

ereplace urban = sum(total_grupo_en) if area==1 & sample==1
ereplace rural = sum(total_grupo_en) if area==2 & sample==1

ereplace male = sum(total_grupo_en) if sexo==1 & sample==1
ereplace female = sum(total_grupo_en) if sexo==2 & sample==1

ereplace ed1 = sum(total_grupo_en) if nivel==1 & sample==1
ereplace ed2 = sum(total_grupo_en) if nivel==2 & sample==1
ereplace ed3 = sum(total_grupo_en) if nivel==3 & sample==1

ereplace age1 = sum(total_grupo_en) if tramo==1 & sample==1
ereplace age2 = sum(total_grupo_en) if tramo==2 & sample==1
ereplace age3 = sum(total_grupo_en) if tramo==3 & sample==1

foreach var of varlist urban-age3 {
	replace `var'=`var'*100/total if sample==0
}
foreach var of varlist urban-age3 {
	replace `var'=`var'*100/total_en if sample==1
}

collapse urban-age3, by(sample)

lab def sample 0 "Census" 1 "Survey", modify
lab val sample sample

lab var ed1 "< Secondary complete" 
lab var ed2 "Secondary complete" 
lab var ed3 "> Secondary complete" 

lab var age1 "18-35" 
lab var age2 "36-54" 
lab var age3 "> 54" 

lab var urban "Urban"
lab var rural "Rural"
lab var female "Female"
lab var male "Male"

bys sample: sum  urban-age3
asdoc sum  urban-age3, by(sample) stats(mean) replace label save($tables/SM4)



